package com.nana.framework.ui.manager
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.utils.Dictionary;

	public class SoundManager
	{
		public static var MAX_LOOP:int = 2147483647;
		private static var _instance:SoundManager;
		
		private var _soundDic:Dictionary;
		private var _soundChannelDic:Dictionary;
		
		public function SoundManager()
		{
			if (_instance == null){
				_instance = this;
				this._soundDic = new Dictionary();
				this._soundChannelDic = new Dictionary();
			} else {
				throw (new Error("实例化单例类出错--SoundManager"));
			}
		}
		
		public static function get instance():SoundManager{
			if (_instance == null){
				_instance = new SoundManager();
			}
			return _instance;
		}
		
		public function registerSound(sound:Sound, _arg2:int):void{
			if (!this._soundDic[_arg2]){
				this._soundDic[_arg2] = sound;
			}
		}
		
		public function playSound(soundId:int, loops:int=0, volume:Number=1, starttime:int=0):SoundChannel{
			if (this._soundDic[soundId]){
				if (this._soundChannelDic[soundId]){
					SoundChannel(this._soundChannelDic[soundId]).stop();
				}
				var transform:SoundTransform = new SoundTransform(volume);
				this._soundChannelDic[soundId] = Sound(this._soundDic[soundId]).play(starttime, loops, transform);
				return this._soundChannelDic[soundId];
			}
			return null;
		}
		
		public function stopSound(_arg1:int):void{
			if (this._soundChannelDic[_arg1]){
				SoundChannel(this._soundChannelDic[_arg1]).stop();
			}
		}
		
		public function getSoundVolume(_arg1:int):Number{
			if (this._soundChannelDic[_arg1]){
				return (SoundChannel(this._soundChannelDic[_arg1]).soundTransform.volume);
			}
			throw (new Error("该声音没有注册或不是播放状态"));
		}
		
		public function setSoundVolume(soundId:int, volume:Number):void{
			if (this._soundChannelDic[soundId]){
				if (volume < 0){
					volume = 0;
				}
				if (volume > 1){
					volume = 1;
				}
				SoundChannel(this._soundChannelDic[soundId]).soundTransform = new SoundTransform(volume);
			} else {
				throw (new Error("该声音没有注册或不是播放状态"));
			}
		}
		
		public function stopAllSound():void{
			if (SoundMixer.areSoundsInaccessible()){
				throw (new Error("无法访问某个播放中的声音"));
			}
			SoundMixer.stopAll();
		}
		
		public function disposeAllSound():void{
			var _local1:String;
			var _local2:String;
			this.stopAllSound();
			for (_local1 in this._soundDic) {
				if (this._soundDic[_local1]){
					this._soundDic[_local1] = null;
					delete this._soundDic[_local1];
				}
			}
			for (_local2 in this._soundChannelDic) {
				if (this._soundChannelDic[_local2]){
					this._soundChannelDic[_local2] = null;
					delete this._soundChannelDic[_local2];
				}
			}
		}

		public function get soundDic():Dictionary
		{
			return _soundDic;
		}

		public function get soundChannelDic():Dictionary
		{
			return _soundChannelDic;
		}

		
	}
}